using System;
using System.Configuration;

namespace NWrapper.Config
{
	/// <summary>
	/// Manages the configuration for NWrapper.
	/// </summary>
	public class Configuration
	{
		private NWrapperSection _settings;

		#region Constructors
		/// <summary>
		/// Constructs using the <c>NWrapper</c> section of the application's default
		/// configuration file.
		/// </summary>
		public Configuration()
			: this(ConfigurationManager.GetSection("NWrapper") as NWrapperSection)
		{
		}

		/// <summary>
		/// Constructs using the given settings.
		/// </summary>
		/// <param name="settings">The settings to use.</param>
		public Configuration(NWrapperSection settings)
		{
			SetDefaultConfiguration(settings);

			_settings = settings;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the underlying NWrapper settings.
		/// </summary>
		public NWrapperSection Settings
		{
			get { return _settings; }
		}
		#endregion

		/// <summary>
		/// Internal method to enforce null proxies where needed.
		/// </summary>
		/// <param name="settings">The settings to adjust.</param>
		private void SetDefaultConfiguration(NWrapperSection settings)
		{
			if (settings.Repositories.Count == 0)
			{
				settings.Repositories.Add(new RepositorySettings("Default", RepositoryType.None));
			}
		}
	}
}
